﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;

namespace BSPortal.Usercontrol.Admin.Book.User
{
    public partial class frmListUser : System.Web.UI.Page
    {
        private short id = 0;
        private MainService service = new MainService();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                LoadCity();
                LoadDistrict();
                LoadWard();

                LoadData();
            }
        }

        protected void LoadCity()
        {
            var mainService = Common.GetClientService();
            var listCity = mainService.GetAllCity().OrderBy(row => row.Name);

            ddlCity.DataValueField = "CityId";
            ddlCity.DataTextField = "Name";
            ddlCity.DataSource = listCity;
            ddlCity.DataBind();
        }

        protected void LoadDistrict()
        {
            if (ddlCity.SelectedValue != string.Empty)
            {
                var mainService = Common.GetClientService();
                var listDistrict = mainService.GetAllDistrict(int.Parse(ddlCity.SelectedValue)).OrderBy(row => row.Name);

                ddlDistrict.DataValueField = "DistrictId";
                ddlDistrict.DataTextField = "Name";
                ddlDistrict.DataSource = listDistrict;
                ddlDistrict.DataBind();
            }
        }

        protected void LoadWard()
        {
            if (ddlDistrict.SelectedValue != string.Empty)
            {
                var mainService = Common.GetClientService();
                var listWard =
                    mainService.GetAllWard(int.Parse(ddlCity.SelectedValue), int.Parse(ddlDistrict.SelectedValue)).OrderBy(row => row.Name);

                ddlWard.DataValueField = "WardId";
                ddlWard.DataTextField = "Name";
                ddlWard.DataSource = listWard;
                ddlWard.DataBind();
            }
        }

        protected void ddlCity_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadDistrict();
        }

        protected void ddlDistrict_SelectedIndexChanged(object sender, EventArgs e)
        {
            LoadWard();
        }

        void LoadData()
        {
            string str = Request["UserId"];
            if (str == null)
            {
                
            }
            else
            {
                note.Text = "(Để trống nếu không muốn đổi password)";
                var data = service.GetUserById(str);
                if (data != null)
                {
                    txtUserName.Text = data.Username;
                    txtName.Text = data.Name;
                    txtAddress.Text = data.Address;
                    txtEmail.Text = data.Email;
                    txtPhone.Text = data.Phone;
                    ddlType.SelectedValue = data.Type.ToString();
                    ddlCity.SelectedValue = data.City.ToString();
                    ddlDistrict.SelectedValue = data.District.ToString();
                    ddlWard.SelectedValue = data.Ward.ToString();
                }
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            if (txtName.Text == "")
            {
                lblStatus.Text = "Tên không được để trống!";
                return;
            }
            if (txtUserName.Text == "")
            {
                lblStatus.Text = "Tên không được để trống!";
                return;
            }
            

            string str = Request["UserId"];

            if (str == null)
            {
                if (txtPass1.Text.Trim() == "" || txtPass2.Text.Trim() == "")
                {
                    lblStatus.Text = "Chưa nhập password!";
                    return;
                }
                if (!txtPass1.Text.Equals(txtPass2.Text))
                {
                    lblStatus.Text = "Password nhập không khớp!";
                    return;
                }
                int create = service.CreateUser(txtUserName.Text, txtPass1.Text, txtName.Text, txtAddress.Text,
                                                txtEmail.Text, txtPhone.Text, int.Parse(ddlType.SelectedValue),
                                                int.Parse(ddlCity.SelectedValue), int.Parse(ddlDistrict.SelectedValue), int.Parse(ddlWard.SelectedValue));
                if (create == (int)Common.RetCode.SUCESS)
                {
                    lblStatus.Text = "Thêm thành công...";
                    hdModified.Value = "true";
                }
                else
                    lblStatus.Text = "Thêm không thành công...";
            }
            else
            {
                if (txtPass1.Text.Trim() != "" || txtPass2.Text.Trim() != "")
                {
                    if (!txtPass1.Text.Equals(txtPass2.Text))
                    {
                        lblStatus.Text = "Password nhập không khớp!";
                        return;
                    }
                    service.ChangePassword(txtUserName.Text, txtPass1.Text);
                }
                int update = service.UpdateUser(txtUserName.Text, txtName.Text, txtAddress.Text, txtEmail.Text,
                                                txtPhone.Text, int.Parse(ddlType.SelectedValue), int.Parse(ddlCity.SelectedValue),
                                                int.Parse(ddlDistrict.SelectedValue), int.Parse(ddlWard.SelectedValue));
                if (update == (int) Common.RetCode.SUCESS)
                {
                    lblStatus.Text = "Cập nhật thành công...";
                    hdModified.Value = "true";
                }
                else
                    lblStatus.Text = "Cập nhật không thành công...";
            }
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            //txtName.Text = string.Empty;
            //txtPrice.Text = string.Empty;
            //ddlParentCategory.SelectedIndex = 0;
            //ddlPriceType.SelectedIndex = 0;
            //ddlCity.SelectedIndex = 0;
        }
       
    }
}